﻿using System;
using Xtensive.Orm;

namespace BinaryFields.Model
{
    /*
     * In some database servers there are 2 data types for storing binary data.
     * For instance, in SQL Server they are called: varbinary and varbinary(max).
     * First one is stored right in data table and therefore has some limitation for column length (e.g. 8k in SQL Server)
     * whereas second is stored separately in blob storage and can contain gigabytes of data.
     * 
     * In domain model field of binary type is represented by byte[] type which can physically contain up to 2 Gb of data.
     * 
     * To map byte[] type to a particular database data type DataObjects.Net uses the following algorithm:
     * 1. If FieldAttribute.Length property is not specified, a column type of varbinary with maximum allowed length will be created.
     * 2. If FieldAttribute.Length property is specified and Length is less than maximum allowed length for varbinary type,
     *    a column type of varbinary with specified Length will be created.
     * 3. If FieldAttribute.Length property is specified and Length exceeds maximum allowed length for varbinary type,
     *    a column type of varbinary(max) will be created.
    */
    [HierarchyRoot]
    public class Movie : Entity
    {
        [Field, Key]
        public int Id { get; set; }

        // Use this to get varbinary(1000) in SQL Server
        [Field(Length = 1000)]
        public byte[] Thumbnail { get; set; }

        // Use this to get varbinary(8000) in SQL Server
        [Field]
        public byte[] Image { get; set; }

        // use this to get varbinary(max) in SQL Server
        [Field(Length = Int32.MaxValue)]
        public byte[] Content { get; set; }

        public Movie(Session session) :
            base(session) {}
    }
}